<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Euclidean distance between polyhedra in 2D</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/eucl_dist_poly_2D.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Euclidean distance between polyhedra in 2D</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.2.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/09/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% Given two polyhedra C = {x | A1*x &lt;= b1} and D = {x | A2*x &lt;= b2}, the</span>
<span class="comment">% distance between them is the optimal value of the problem:</span>
<span class="comment">%           minimize    || x - y ||_2</span>
<span class="comment">%               s.t.    A1*x &lt;= b1</span>
<span class="comment">%                       A2*y &lt;= b2</span>
<span class="comment">% Note: here x is in R^2</span>

<span class="comment">% Input data</span>
randn(<span class="string">'seed'</span>,0);
n = 2;
m = 2*n;
A1 = randn(m,n);
b1 = randn(m,1);
A2 = randn(m,n);
b2 = randn(m,1);

fprintf(1,<span class="string">'Computing the distance between the 2 polyhedra...'</span>);
<span class="comment">% Solution via CVX</span>
cvx_begin
    variables <span class="string">x(n)</span> <span class="string">y(n)</span>
    minimize (norm(x - y))
    norm(x,1) &lt;= 2;
    norm(y-[4;3],inf) &lt;=1;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------'</span>);
disp(<span class="string">'The distance between the 2 polyhedra C and D is: '</span> );
disp([<span class="string">'dist(C,D) = '</span> num2str(cvx_optval)]);
disp(<span class="string">'The optimal points are: '</span>)
disp(<span class="string">'x = '</span>); disp(x);
disp(<span class="string">'y = '</span>); disp(y);

<span class="comment">%Plotting</span>
figure;
fill([-2; 0; 2; 0],[0;2;0;-2],<span class="string">'b'</span>, [3;5;5;3],[2;2;4;4],<span class="string">'r'</span>)
axis([-3 6 -3 6])
axis <span class="string">square</span>
hold <span class="string">on</span>;
plot(x(1),x(2),<span class="string">'k.'</span>)
plot(y(1),y(2),<span class="string">'k.'</span>)
plot([x(1) y(1)],[x(2) y(2)])
title(<span class="string">'Euclidean distance between 2 polyhedron in R^2'</span>);
xlabel(<span class="string">'x_1'</span>);
ylabel(<span class="string">'x_2'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the distance between the 2 polyhedra... 
Calling sedumi: 15 variables, 5 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 5, order n = 15, dim = 16, blocks = 2
nnz(A) = 23 + 0, nnz(ADA) = 17, nnz(L) = 12
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            5.33E+00 0.000
  1 :  -1.07E-01 1.65E+00 0.000 0.3105 0.9000 0.9000   2.24  1  1  2.6E+00
  2 :   1.35E+00 4.81E-01 0.000 0.2907 0.9000 0.9000   0.96  1  1  7.8E-01
  3 :   2.01E+00 1.02E-01 0.000 0.2131 0.9000 0.9000   1.34  1  1  1.4E-01
  4 :   2.12E+00 3.39E-03 0.000 0.0331 0.9900 0.9900   1.05  1  1  4.5E-03
  5 :   2.12E+00 2.90E-04 0.414 0.0855 0.9900 0.9900   1.00  1  1  3.8E-04
  6 :   2.12E+00 5.11E-06 0.000 0.0176 0.9900 0.9903   1.00  1  1  1.3E-05
  7 :   2.12E+00 3.04E-08 0.327 0.0060 0.9966 0.9990   1.00  1  1  5.9E-08
  8 :   2.12E+00 9.48E-10 0.000 0.0312 0.9900 0.9902   1.00  1  1  2.2E-09

iter seconds digits       c*x               b*y
  8      0.1   9.8  2.1213203395e+00  2.1213203392e+00
|Ax-b| =   8.0e-09, [Ay-c]_+ =   0.0E+00, |x|=  3.7e+00, |y|=  1.6e+00

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    1.000E-01    0.000E+00    
Max-norms: ||b||=4, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.12132
Done! 
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is: 
dist(C,D) = 2.1213
The optimal points are: 
x = 
    1.5000
    0.5000

y = 
    3.0000
    2.0000

</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="eucl_dist_poly_2D__01.png" alt=""> 
</div>
</div>
</body>
</html>